package com.liuwei.spring.cloud.common.utils.feishu.message;/**
 * @author : MarkdownMessage
 * @version V1.0
 * @date: 2023/5/24 6:08 下午
 * @Description: TODO
 */

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : MarkdownMessage
 * @version V1.0
 * @date: 2023/5/24 6:08 下午
 * @Description: TODO
 */
@Data
public class RichMessage extends BaseFeishuMessage {

    /**
     * {
     * 	"msg_type": "post",
     * 	"content": {
     * 		"post": {
     * 			"zh_cn": {
     * 				"title": "项目更新通知",
     * 				"content": [
     * 					[{
     * 							"tag": "text",
     * 							"text": "项目有更新: "
     *                                                },
     *                        {
     * 							"tag": "a",
     * 							"text": "请查看",
     * 							"href": "http://www.example.com/"
     *                        },
     *                        {
     * 							"tag": "at",
     * 							"user_id": "ou_18eac8********17ad4f02e8bbbb"
     *                        }
     * 					]
     * 				]
     * 			}
     * 		}
     * 	}
     * }
     */
    @JsonProperty("msg_type")
    private String messageType = "post";

    private MessageContent content;

    @Data
    public static class MessageContent{
        private MessagePost post;
    }

    @Data
    public static class MessagePost{
        @JsonProperty("zh_cn")
        private MessageCn zhcn;
    }

    @Data
    public static class MessageCn{
        private String title;
        private List<List<LineItem>> content;
    }

    @Data
    public static class TextItem implements LineItem{
        private String tag = "text";
        private String text;
        public TextItem(String text){
            this.text = text;
        }
    }

    @Data
    public static class HrefItem implements LineItem{
        private String tag = "a";
        private String text;
        private String href;
        public HrefItem(String text,String href){
            this.text = text;
            this.href = href;
        }
    }

    @Data
    public static class AtItem implements LineItem{
        private String tag = "at";
        @JsonProperty("user_id")
        private String userId;
        public AtItem(String userId){
            this.userId = userId;
        }
    }

    @Data
    public static class ImageItem implements LineItem{
        private String tag = "img";
        @JsonProperty("image_key")
        private String imageKey;

        public ImageItem(String key){
            this.imageKey = key;
        }
    }

    public  interface LineItem{}

    public static RichMessage buildRichMessage(String title, List<List<LineItem>> rows){
        RichMessage message = new RichMessage();
        message.setMessageType("post");

        MessageContent content = new MessageContent();
        message.setContent(content);

        MessagePost post = new MessagePost();
        content.setPost(post);

        MessageCn cn = new MessageCn();
        post.setZhcn(cn);

        cn.setTitle(title);
        cn.setContent(rows);
        return message;
    }
}
